﻿using AutoTask.Collect.Model;
using AutoTask.Model;
using RpcHelper;
using RpcHelper.Error;

namespace AutoTask.Service.Helper
{
    internal static class LinqHelper
    {
        public static TaskExecLog[] ExecTask(this AutoTaskItem item, int regionId)
        {
            return TaskItemHelper.ExecTask(item, regionId);
        }
        public static DateTime GetExecTime(this AutoTaskPlan[] plans, DateTime? execTime)
        {
            if (plans.Length == 1)
            {
                return PlanHepler.GetExecTime(plans[0], execTime);
            }
            DateTime minTime = DateTime.MaxValue;
            DateTime now = DateTime.Now;
            plans.ForEach(c =>
            {
                DateTime time = PlanHepler.GetExecTime(c, execTime);
                if (time < minTime)
                {
                    minTime = time;
                }
            });
            return minTime;
        }
        public static DateTime GetNextTime(this AutoTaskPlan[] plans, DateTime execTime)
        {
            if (plans.Length == 1)
            {
                return PlanHepler.GetNextTime(plans[0], execTime);
            }
            DateTime minTime = DateTime.MaxValue;
            DateTime now = DateTime.Now;
            plans.ForEach(c =>
            {
                DateTime time = PlanHepler.GetNextTime(c, execTime);
                if (time < minTime)
                {
                    minTime = time;
                }
            });
            return minTime;
        }
        public static bool CheckPlan(this AutoTaskPlan[] plans)
        {
            bool isEnable = true;
            plans.ForEach(a =>
            {
                if (!DataValidateHepler.ValidateData(a, out string error))
                {
                    a.ErrorId = LocalErrorManage.GetErrorId(error);
                    a.IsEnable = false;
                    isEnable = false;
                }
            });
            return isEnable;
        }

    }
}
